package com.mall.server.controller.yxcontroller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.FlashService;
import com.tanhua.model.domain.SmsFlashPromotion;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/flash")
public class FlashController {

    @Autowired
    private FlashService flashService;

    /**
     * 根据活动名称分页查询
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R flashList(String keyword, @RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        PageVo pageVo =flashService.flashList(keyword,pageNum,pageSize);
        return R.success(pageVo);
    }

    /**
     * 修改秒杀上下线
     * @param id
     * @param status
     * @return
     */
    @PostMapping("update/status/{id}")
    public R updateStatus(@PathVariable("id") Long id,Integer status){
        if (id == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashService.updateStatus(id,status);
        return R.success("");
    }

    /**
     * 添加秒杀活动
     * @param smsFlashPromotion
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody SmsFlashPromotion smsFlashPromotion){
        if (smsFlashPromotion == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        //开始时间大于结束时间 出异常
        if (smsFlashPromotion.getStartDate().getTime() > smsFlashPromotion.getEndDate().getTime()){
            throw new BusinessException(ErrorResult.timeError());
        }

        //开始时间小于当前时间 出异常
        if (smsFlashPromotion.getStartDate().getTime() < System.currentTimeMillis()){
            throw new BusinessException(ErrorResult.timeError());
        }
        flashService.create(smsFlashPromotion);
        return R.success("");
    }

    /**
     * 编辑秒杀活动
     * @param id
     * @param smsFlashPromotion
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable("id")Long id,@RequestBody SmsFlashPromotion smsFlashPromotion){
        if (id == null){
            throw new BusinessException(ErrorResult.parameterError());
        }

        if (smsFlashPromotion.getStartDate() != null){
            if (smsFlashPromotion.getStartDate().getTime() < System.currentTimeMillis()){
                throw new BusinessException(ErrorResult.timeError());

            }
        }

        if (smsFlashPromotion.getEndDate() != null){
            if (smsFlashPromotion.getEndDate().getTime() < System.currentTimeMillis()){
                throw new BusinessException(ErrorResult.timeError());
            }
        }

        if (smsFlashPromotion.getStartDate() != null && smsFlashPromotion.getEndDate() != null){
            //开始时间大于结束时间 出异常
            if (smsFlashPromotion.getStartDate().getTime() > smsFlashPromotion.getEndDate().getTime()){
                throw new BusinessException(ErrorResult.timeError());
            }

            //开始时间小于当前时间 出异常
            if (smsFlashPromotion.getStartDate().getTime() < System.currentTimeMillis()){
                throw new BusinessException(ErrorResult.timeError());
            }
        }

        flashService.update(smsFlashPromotion);

        return R.success("");
    }


    /**
     * 删除活动
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        flashService.delete(id);
        return R.success("");
    }


}
